/*
  Copyright Shmatoff.info, 2008.
  Script for small TGP.

*/
<?php
    
    include "sets.php";
    include "config.php";
        
    header("Pragma: no-cache");
    
    $_v = array();
    $_c = array();
    foreach($_GET as $k => $v) $_v[$k]=$v;
    foreach($_POST as $k => $v) $_v[$k]=$v;
    foreach($_COOKIE as $k => $v) $_c[$k]=$v;

    $cdir = ''; //Текущий каталог    
    $_title = $title; //Полный вариант заголовка
    $_stylesheet = $stylesheet;
    $_keywords = $keywords;
    $_description = $description;
    $keylist = split(",", $keywords); //массив кеев для использования в качестве альтов
    $_v['dirs'] = (isset($_v['dirs']) ? $_v['dirs'] : "");
    $css = "";
    $keys = "";
    $advblocks = array();

    if(file_exists($data_path.$_dirlist)){
        $_dirs = file($data_path.$_dirlist);
    }
    else{
        $_dirs = array();
    }

    if(!($_v['dirs']) == "")
    {
        $_t = 0;
        foreach($_dirs as $tmp){
            list($k, $v, $css, $aurl, $keys)= split("[|]", $tmp);
            if($k == $_v['dirs']){
                $cdir = $k;
                $_title = $v.' : '.$title;
                break;
            }
        }
    } //if(!($_v['dirs']) == "")
    
    $_thumbs = array(); //Заготовки тумб для выборки в листы
    
    if($cdir == ''){ //Обработка полного списка
        foreach($_dirs as $tmp){
            list($k, $v, $css, $aurl, $keys) = split('[|]', $tmp);
            if(file_exists($data_path.$k)){
                $_f = file($data_path.$k);
                $_keywords = $_keywords.','.trim($v);
            }
            else{
                $_f = array();
            }
            
            if($keys){
                $kl = array_merge($keylist, split(",", $keys));
            }
            else{
                $kl = $keylist;
            }
            
            $_target = $open_in_new?"target='_blank'":" ";
            if(file_exists($data_path.$k."aimg")){
                $_adv = file($data_path.$k."aimg");
                if(file_exists($data_path.$k."atxt")){
                    $_atxt = file($data_path.$k."atxt");
                }
                foreach($_adv as $tmp){
                    shuffle($_atxt);
                    $advblocks[] =array("<div class='rbanner'><a href='$aurl' $_target>","<img src='$tmp' alt='$v'>",
                      "<br/>$_atxt[0]","</a></div>"); 
                }
            }
            
            foreach($_f as $tmp){
                list($tumb, $gurl, $desc) = split('[|]', $tmp);
                shuffle($kl);
                $_thumbs[] = "<a href='$gurl' $_target><img src='$tumb' alt='$kl[0]' title='$desc' /></a>";
            }
        }
    } // if $cdir == ''
    else{ //обработка выбранной категории
        $_f = file($data_path.$cdir);
        if($keys){
            $keylist = split(",", $keys);
            $_keywords = $keys;
        }
        if(($css) && (file_exists($css))){
            $_stylesheet = $css;
        }
        
        $_target = $open_in_new?"target='_blank'":" ";
        
        if(file_exists($data_path.$k."aimg")){
            $_adv = file($data_path.$k."aimg");
            if(file_exists($data_path.$k."atxt")){
                $_atxt = file($data_path.$k."atxt");
            }
            foreach($_adv as $tmp){
                shuffle($_atxt);
                $advblocks[] =array("<div class='rbanner'><a href='$aurl' $_target>","<img src='$tmp' alt='$v'>",
                  "<br/>$_atxt[0]","</a></div>"); 
            }
        }

        foreach($_f as $tmp){
            list($tumb, $gurl, $desc) = split('[|]', $tmp);
            shuffle($keylist);
            $_thumbs[] = "<a href='$gurl' $_target><img src='$tumb' alt='$keylist[0]' title='$desc' /></a>";
        }
    }
    
    shuffle($_thumbs);
    
    $advlist = file($data_path.$_advert_links);
    foreach($advlist as $tmp){
        $advblocks[] = file(trim($tmp));
    }
    while(count($advblocks) < ($bloks_on_page + 1)){
        echo "<p>array merge</p>";
        $advblocks = array_merge($advblocks, $advblocks);
    }
    shuffle($advblocks);
    
    include $_tpl['header'];
    print("<div class='content'>");
    print("<div class='thumbs'>");
    print implode("", $advblocks[0]);
    for($i = 0; $i < ($bloks_on_page+1); $i++){
        print("<div class='thumbblock'>");
        for($y=0; $y < $rows_in_block; $y++){
            print("<div class='thumbrow'>");
            for($z = 0; $z < $thumb_in_row; $z++){
                if($_t = next($_thumbs)){
/*k                    print($_t); /**/
                }
            }
            print("</div>"); //Thumb row
        }
        print("</div>"); //Thumb block
        print implode("", $advblocks[$i+1]);
    }
    print("</div>"); //class = thumbs
    
    $mdir = ($cdir)?" ":"id='curmenu'";
    $_catmenu = array();
    $_catmenu[] = "<ul id='menu'><li $mdir><a href='$url'>$title</a></li>";
    foreach($_dirs as $tmp){
        list($k, $v) = split('[|]', $tmp);
        $mid = ($k==$cdir)?"id='curmenu'":" ";
        $_t = "<li $mid><a href='?dirs=$k'>$v</a></li>";
        $_catmenu[] = $_t;
    } /**/

    $_catmenu[] = "</ul>";

//    echo "menu -> " + htmlspecialchars($_catmenu) + "<br/>";
    
    include $_tpl['leftcol'];
    print("</div>"); // class = content

    include $_tpl['footer'];    
?>
